//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.Caching;
using CommunityServer.Components;


namespace CommunityServer {

	public class UrlRedirects {

		public static ArrayList GetUrlRedirects() {
			
			string key = CacheKey;
			ArrayList urlRedirects = CSCache.Get(key) as ArrayList;
			if(urlRedirects == null)
			{
				CommonDataProvider dp = CommonDataProvider.Instance();
				urlRedirects = dp.GetUrlRedirects();
				CSCache.Insert(key,urlRedirects,15*CSCache.MinuteFactor,CacheItemPriority.Low);
			}

			return urlRedirects;
		}

		private static string CacheKey
		{
			get { return "UrlRedirects:" + CSContext.Current.SiteSettings.SettingsID; }
		}

		public static int CreateUrlRedirect( UrlRedirect urlRedirect ) {
			CSCache.Remove(CacheKey);
			return CreateUpdateDeleteUrlRedirect( urlRedirect, DataProviderAction.Create);
		}
		public static int DeleteUrlRedirect( UrlRedirect urlRedirect ) {
			CSCache.Remove(CacheKey);
			return CreateUpdateDeleteUrlRedirect( urlRedirect, DataProviderAction.Delete);
		}
		public static int UpdateUrlRedirect( UrlRedirect urlRedirect ) {
			CSCache.Remove(CacheKey);
			return CreateUpdateDeleteUrlRedirect( urlRedirect, DataProviderAction.Update);
		}

		private static int CreateUpdateDeleteUrlRedirect( UrlRedirect urlRedirect, DataProviderAction action ) {
		    CSCache.Remove(CacheKey);
			CommonDataProvider dp = CommonDataProvider.Instance();
			return dp.CreateUpdateDeleteUrlRedirect( urlRedirect, action );
		}
	}
}